package com.tan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {

    // 使用redis时候 需要对存储的值 进行序列号 否则会乱码
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 将template泛型设置为 string, object
        final RedisTemplate<String, Object> template = new RedisTemplate<>();

        // redis连接工厂
        template.setConnectionFactory(redisConnectionFactory);

        final Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

        template.setKeySerializer(RedisSerializer.java());
        template.setHashKeySerializer(RedisSerializer.java());

        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);

        return template;
    }

}
